<?php

	$type = @$_REQUEST['type'];

	//$type = 'getOpenid';

	switch ($type) {


		default:
			echo 'default';

			break;


		//===== 后台管理端接口



		//从审核列表中去除无效项
		case 'removecheckInfo':
			$cityCode = @$_POST['cityCode'];
			$id = @$_POST['id'];
			_writePro();	//写保护
			echo _checkInfoListForRemove($cityCode,$id);
			_writePro(1);	//清除写保护
			exit;
		break;


		//审核信息
		case 'agree':
			$cityCode = @$_POST['cityCode'];
			$id = @$_POST['id'];
			$val = @$_POST['val'];
			if(!$cityCode || !$id || !$val){
				exit('err1');
			}
			//dump($_POST,1);
			_writePro();	//写保护
			//sleep(2);
			$path = './data/'.$cityCode;
			$pathTag = './data/'.$cityCode.'_tag';

			//修改城市信息
			if(file_exists($path)){
				$res = json_decode(F($path),1);
				for($i=0;$i<count($res['list']);$i++){
					if($res['list'][$i][0] == $id){
						$res['list'][$i][6] = $val;
					}
				}
				//修改版本号
				$tag = microtime(1);
				$res['tag'] = $tag;
				
				F($pathTag,$tag);
				F($path,json_encode($res));
				echo _checkInfoListForRemove($cityCode,$id);
			}else{
				echo 'err2';
			}
			_writePro(1);	//清除写保护
			exit;
		break;

		//修改状态时记得要做文件写保护





		//===== 小程序端接口

		//用户删除信息
		case 'infoDel':
			$id = @$_POST['id'];
			$cityCode = @$_POST['cityCode'];
			if(!$id || !$cityCode){
				exit('err1');	//缺少参数
			}
			//dump($_POST,1);
			_writePro();	//写保护
			sleep(1);
			$path = './data/'.$cityCode;
			$pathTag = './data/'.$cityCode.'_tag';
			if(!file_exists($path)){
				exit('err2');	//城市文件不存在 
			}

			$res = json_decode(F($path),1);
			$tmpArr = [];
			$stat = false;
			for($i=0;$i<count($res['list']);$i++){
				if($res['list'][$i][0] == $id){
					$stat = true;
				}else{
					$tmpArr[] = $res['list'][$i];
				}
			}
			if($stat){
				$tag = microtime(1);
				$res['list'] = $tmpArr;
				$res['tag'] = $tag;
				F($path,json_encode($res));
				F($pathTag,$tag);
				_writePro(1);	//写保护
				exit('ok');
			}else{
				exit('err3');	//没有数据可删
			}

			break;


		case 'getOpenid':
			//echo '{"session_key":"D2q91F0Lk5tzQ4BT5KBhqQ==","expires_in":7200,"openid":"oaT0N0aqb36we0z7ONiCF5Ai5sQU"}';exit;
			$code = @$_POST['code'];
			//$code = '061BDp0w3L08GW2Dwv2w3Sd7uL2BDp0M';

			$appid = 'wx6e4d450b105c0173';
			$secret = '3d6c29c6268c6e2d869a08460b136ce0';
			$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
			exit(Curls($url));
			break;





		//添加信息		* 记得要做文件写保护
		case 'addInfo':

			//dump($_POST);
			$title = @$_POST['title'];
			$content = @$_POST['content'];
			$concat = @$_POST['concat'];
			$phone = @$_POST['phone'];
			$infoType = @$_POST['infoType'];
			$cityCode = @$_POST['cityCode'];
			$openid = @$_POST['openid'];
			//dump($_POST,1);
			if(!$title || !$content || !$concat || !$phone || !$infoType || !$cityCode || !$openid){
				exit('err');
			}
			_writePro();	//写保护
			//sleep(2);
			$stat = 1;
			$ctime = time();
			$id = md5('a'.$cityCode.microtime(1).rand(1,999999999));
			$arr = array(
				$id,
				$title,
				$content,
				$concat,
				$phone,
				$infoType,
				$stat,
				$ctime,
				$openid
			);
			$path = './data/'.$cityCode;
			if(file_exists($path)){
				//文件存在则追加
				$list = json_decode(file_get_contents($path),1);
				//dump($list,1);
				$counts = 0;
				for($i=0;$i<count($list['list']);$i++){
					if($list['list'][$i][8] == $openid){
						$counts += 1;
					}
				}
				if($counts > 120){
					_writePro(1);	//清除写保护
					exit('err');	//发布条数超限
				}		
				array_push($list['list'], $arr);
			}else{
				//文件不存在则创建并添加
				$list = array(
					'list'=>array($arr),
					'tag'=>0
				);
			}
			F($path,json_encode($list));

			//记录新发布的信息，待审核使用
			$tagInfoTmp = array(
				$cityCode,
				$id,
				$title
			);
			$tagPath = './checkInfoList';
			if(file_exists($tagPath)){
				$tagInfos = json_decode(F($tagPath));
				array_push($tagInfos,$tagInfoTmp);

			}else{
				$tagInfos = array($tagInfoTmp);
			}
			F($tagPath,json_encode($tagInfos));
			_writePro(1);	//清除写保护
			$backData = array(
				'stat'=>'ok',
				'data'=>$arr
			);
			exit(json_encode($backData));
			break;

	}





//写保护
function _writePro($type=false){
	$tagCheckPath = './tag_check';
	if($type){
		//清除写保护
		if(@file_exists($tagCheckPath)){
			unlink($tagCheckPath);
		}

	}else{
		//设置写保护
		if(@file_exists($tagCheckPath)){
			exit('wait');
		}else{
			F($tagCheckPath,1);
		}
	}
}





function dump($r,$type=false){
	echo '<pre>';
	var_dump($r);
	echo '</pre>';
	if($type){
		exit;
	}
}



/*
	### CURL 抓取指定地址内容(get方式)
	参一：$url   string   要抓取的地址
	参二：$code  string   编码方式.如：'gbk' 。默认为 'utf-8'。
	return：返回抓取到的内容(string形式)
	示例：
		$url = 'https://xuewei.supfree.net';
		echo Curls($url,'gbk');exit;
*/
function Curls($url,$code = 'utf-8'){
	header('content-type:text/html;charset='.$code);
	$curlobj = curl_init();
	curl_setopt($curlobj,CURLOPT_URL,$url);
	curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, FALSE);
	$res = curl_exec($curlobj);
	curl_close($curlobj);
	return $res;
}

/*
	### 递归创建目录
	### 参一：目录路径
*/
function Mkdirs($p){
	if(!is_dir($p)){
		Mkdirs(dirname($p),0777,true);
		mkdir($p);
	}
}




//递归删除目录及文件
function DelDirs($p){
	if(is_file($p)){
		unlink($p);
	}
	if(is_dir($p)){
		$op = opendir($p);
		while($rd = readdir($op)){
			if($rd == '.' || $rd == '..'){
				continue;
			}
			$p2 = $p.'/'.$rd;
			DelDirs($p2);
		}
		rmdir($p);
		closedir($op);
	}
}




/*
	### 向指定文件写入内容(自动创建不存在目录)
	参一：$psth (string) 	要写入文件的路径
	参二：$content (string) 	要写入的内容
	示例：
*/
//function WriteFile($path,$content){
function F($path,$content=false){
	if(func_num_args() > 1){	//存储数据
		$dir = dirname($path);
		if(!is_dir($dir)){
			Mkdirs($dir);
		}
		file_put_contents($path,$content);
	}else{ 						//获取数据
		if(file_exists($path)){
			return file_get_contents($path);
		}
	}
}



/*
	## 从审核列表中去除指定项
	返回值：
		成功 ok
		失败 err
	示例：
		echo _checkInfoListForRemove($cityCode,$id);
*/
function _checkInfoListForRemove($cityCode,$id){
	$pathCheck = './checkInfoList';
	if(file_exists($pathCheck)){
		$resCheck = json_decode(F($pathCheck),1);
		$resCheck2 = array();
		for($i=0;$i<count($resCheck);$i++){
			if($resCheck[$i][0] == $cityCode && $resCheck[$i][1] == $id){
				continue;
			}
			$resCheck2[] = $resCheck[$i];
		}
		F($pathCheck,json_encode($resCheck2));
		return 'ok';
	}else{
		return 'err';
	}
}
